package com.qf.events.listeners;

import com.qf.enums.LoginResultEnum;
import com.qf.events.MyLoginEvent;
import com.qf.mapper.SysLogininforMapper;
import com.qf.mapper.SysUserOnlineMapper;
import com.qf.pojo.SysLogininfor;
import com.qf.pojo.SysUserOnline;
import com.qf.utils.RequestUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * created by 赵文韬 on 2022/3/19 13:51
 */
//创建
    @Component
public class LoginEventListener {
    //将用户在线的mapper和系统数据的dao导入进来并注入
    private SysLogininforMapper sysLogininforMapper;
    private SysUserOnlineMapper sysUserOnlineMapper;
    @Autowired
    public void setSysLogininforMapper(SysLogininforMapper sysLogininforMapper) {
        this.sysLogininforMapper = sysLogininforMapper;
    }
    @Autowired
    public void setSysUserOnlineMapper(SysUserOnlineMapper sysUserOnlineMapper) {
        this.sysUserOnlineMapper = sysUserOnlineMapper;
    }
    //监听事件，写一个onEvent方法，参数是MyLoginEvent类型
    @EventListener
    public void onEvent(MyLoginEvent myLoginEvent){
        //获取request对象
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        //接下来的操作都需要异步执行
        CompletableFuture.runAsync(()->{

            //从request中获取所有数据保存到SysLogininfor对象中
            HttpServletRequest request = requestAttributes.getRequest();

            //把SysLogininfor对象中的数据保存到数据库
            String sessionId = request.getSession().getId();
            String ip = RequestUtil.getRemoteHost(request);
            Map<String, String> osAndBrowserInfo = RequestUtil.getOsAndBrowserInfo(request);
            String locationByIp = RequestUtil.getLocationByIp(ip);
            SysLogininfor sysLogininfor = new SysLogininfor();
            sysLogininfor.setIpaddr(ip);
            sysLogininfor.setLoginLocation(locationByIp);
            sysLogininfor.setLoginName(myLoginEvent.getUserName());
            sysLogininfor.setLoginTime(new Date());
            sysLogininfor.setBrowser(osAndBrowserInfo.get("browser"));
            sysLogininfor.setOs(osAndBrowserInfo.get("os"));
            switch (myLoginEvent.getResultEnum()){
                case SUCCESS:
                    sysLogininfor.setMsg("登录成功");
                    sysLogininfor.setStatus("1");
                    break;
                case FAIL:
                    sysLogininfor.setMsg("登录失败");
                    sysLogininfor.setStatus("0");
                    break;
            }
            sysLogininforMapper.addLoginLog(sysLogininfor);
            //判断是否登录成功，成功的画更新在线用户
            if (myLoginEvent.getResultEnum()== LoginResultEnum.SUCCESS){
                SysUserOnline sysUserOnline = new SysUserOnline();
                BeanUtils.copyProperties(sysLogininfor,sysUserOnline );
                sysUserOnline.setSessionId(sessionId);
                sysUserOnline.setExpireTime(30L);
                sysUserOnline.setStartTimestamp(new Date());
                System.err.println(sysUserOnline);
                //保存到数据库
                sysUserOnlineMapper.addSysUserOnline(sysUserOnline);
            }

        });
    }


}
